package com.wrench.elevator.base.user.service;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.wrench.elevator.base.user.dao.BuildingDao;
import com.wrench.elevator.base.user.domain.Building;
import com.wrench.elevator.constant.Result;
import com.wrench.elevator.exception.WrenchException;
import com.wrench.elevator.util.WrenchUtil;

/**
 * @Description: BuildingService
 * @author yangchenfeng
 * @date Oct 19, 2017 10:21:28 AM
 * @version V1.0
 */
@Component
public class BuildingService
{
    @Resource
    private BuildingDao dao;
    
    private static final Logger logger = LoggerFactory.getLogger(BuildingService.class);
    
    /**
     * 获取楼盘
     * */
    public String getBuildings()
        throws WrenchException
    
    {
        String buildingsStr = null;
        List<Map> buildings = null;
        
        try
        {
            buildings = dao.getBuildings();
        }
        catch (Exception e)
        {
            logger.error("==> buildings = dao.getBuildings()  execute error ,buildings = {} , error = {}; ",
                buildings,
                e);
            throw new WrenchException(Result.DB_SELECT_ERROR_CODE, Result.DB_SELECT_ERROR_MESSAGE);
        }
        
        if (!WrenchUtil.hasNull(buildings))
        {
            buildingsStr = JSON.toJSONString(buildings);
        }
        
        logger.debug("==> buildings = dao.getBuildings() execute success , buildings = {}", buildingsStr);
        return buildingsStr;
    }
    
    /**
     * 获取group 编号
     * */
    public String getGroupId(String building_id)
        throws WrenchException
    {
        
        if (WrenchUtil.hasNull(building_id))
        {
            logger.error("==> param is not found , building_id = {} ", building_id);
            throw new WrenchException(Result.PARAM_NOT_FOUND_CODE, Result.PARAM_NOT_FOUND_MESSAGE);
        }
        
        if (!WrenchUtil.isInt(building_id))
        {
            logger.error("==> string is not int , building_id = {} ", building_id);
            throw new WrenchException(Result.STRING_IS_NOT_INT_CODE, Result.STRING_IS_NOT_INT_MESSAGE);
        }
        
        String groupId = null;
        
        try
        {
            groupId = dao.getGroupId(Integer.valueOf(building_id));
        }
        catch (Exception e)
        {
            logger.error("==>  groupId=dao.getGroupId(Integer.valueOf(building_id))  execute error , building_id = {} , error = {}; ",
                building_id,
                e);
            throw new WrenchException(Result.DB_SELECT_ERROR_CODE, Result.DB_SELECT_ERROR_MESSAGE);
        }
        
        logger.debug("==> groupId=dao.getGroupId(Integer.valueOf(building_id)) execute success , building_id = {} , group_id = {} ",
            building_id,
            groupId);
        
        return groupId;
    }
    
    /**
     * 设置group 编号
     * */
    public int setGroupId(String building_id, String group_id)
        throws WrenchException
    {
        if (WrenchUtil.hasNull(building_id, group_id))
        {
            logger.error("==> param is not found , building_id = {} , group_id = {}  ", building_id);
            throw new WrenchException(Result.PARAM_NOT_FOUND_CODE, Result.PARAM_NOT_FOUND_MESSAGE);
        }
        
        if (!WrenchUtil.isInt(building_id))
        {
            logger.error("==> string is not int , building_id = {} ", building_id);
            throw new WrenchException(Result.STRING_IS_NOT_INT_CODE, Result.STRING_IS_NOT_INT_MESSAGE);
        }
        
        int commit = -1;
        try
        {
            commit = dao.setGroupId(Integer.valueOf(building_id), group_id);
        }
        catch (Exception e)
        {
            logger.error("==> commit=dao.setGroupId(Integer.valueOf(building_id), group_id) execute error , building_id = {} , group_id = {} , error = {}; ",
                building_id,
                group_id,
                e);
            throw new WrenchException(Result.DB_UPDATE_ERROR_CODE, Result.DB_UPDATE_ERROR_MESSAGE);
        }
        
        if (commit <= 0)
        {
            logger.error("==> commit=dao.setGroupId(Integer.valueOf(building_id), group_id) execute success , but commit <=0 , commit = {} ,  building_id = {} , group_id = {}  ",
                commit,
                building_id,
                group_id);
            throw new WrenchException(Result.BUILDING_SET_GROUP_ID_FAIL_CODE, Result.BUILDING_SET_GROUP_ID_FAIL_MESSAGE);
        }
        
        logger.debug("==> commit=dao.setGroupId(Integer.valueOf(building_id), group_id) execute success ,  building_id = {} , group_id = {}  , commit = {} ",
            building_id,
            group_id,
            commit);
        
        return commit;
    }
    
    /**
     * 根据物业编号获取该物业的所有楼盘
     * */
    public List<Map> getBuildingsByPropertyId(String property_id)
        throws WrenchException
    {
        if (WrenchUtil.hasNull(property_id))
        {
            logger.error("==> param is not found , property_id = {} ", property_id);
            throw new WrenchException(Result.PARAM_NOT_FOUND_CODE, Result.PARAM_NOT_FOUND_MESSAGE);
            
        }
        
        property_id = property_id.trim();
        
        if (!WrenchUtil.isInt(property_id))
        {
            logger.error("==> string is not int , property_id = {} ", property_id);
            throw new WrenchException(Result.STRING_IS_NOT_INT_CODE, Result.STRING_IS_NOT_INT_MESSAGE);
        }
        
        List<Map> values = null;
        try
        {
            values = dao.getBuildingsByPropertyId(Integer.valueOf(property_id));
        }
        catch (Exception e)
        {
            logger.error("==>  values = dao.getBuildingsByPropertyId(Integer.valueOf(property_id)) execute error , property_id = {} , error = {} ",
                property_id,
                e);
            throw new WrenchException(Result.DB_SELECT_ERROR_CODE, Result.DB_SELECT_ERROR_MESSAGE);
        }
        
        logger.debug("==>  values = dao.getBuildingsByPropertyId(Integer.valueOf(property_id)) execute success , property_id = {} , values = {}  ",
            property_id,
            values);
        
        return values;
    }
    
    /**
     * 添加楼盘
     * 
     * @throws WrenchException
     * */
    public int addBuilding(String province_id, String province_name, String city_id, String city_name,
        String district_id, String district_name, String street_name, String building_name,
        String building_zhuangshu_danyuan, String elevator_num, String property_id)
        throws WrenchException
    {
        if (WrenchUtil.hasNull(province_id,
            province_name,
            city_id,
            city_name,
            district_id,
            district_name,
            street_name,
            building_name,
            building_zhuangshu_danyuan,
            elevator_num,
            property_id))
        {
            logger.error("==> param is not found , province_id = {} , province_name = {} , city_id = {} , city_name = {} , district_id = {} , district_name = {} , street_name = {} , building_name = {} , building_zhuangshu_danyuan = {} , elevator_num = {} , property = {} ",
                province_id,
                province_name,
                city_id,
                city_name,
                district_id,
                district_name,
                street_name,
                building_name,
                building_zhuangshu_danyuan,
                elevator_num,
                property_id);
            throw new WrenchException(Result.PARAM_NOT_FOUND_CODE, Result.PARAM_NOT_FOUND_MESSAGE);
            
        }
        province_id = province_id.trim();
        province_name = province_name.trim();
        city_id = city_id.trim();
        city_name = city_name.trim();
        district_id = district_id.trim();
        district_name = district_name.trim();
        street_name = street_name.trim();
        building_zhuangshu_danyuan = building_zhuangshu_danyuan.trim();
        building_name = building_name.trim();
        elevator_num = elevator_num.trim();
        property_id = property_id.trim();
        
        if (!WrenchUtil.areInt(province_id, city_id, district_id, elevator_num, property_id))
        {
            logger.error("==> string is not int , province_id = {} , city_id = {} , district_id = {} , elevator_num = {} , property_id = {} ",
                province_id,
                city_id,
                district_id,
                elevator_num,
                property_id);
            throw new WrenchException(Result.STRING_IS_NOT_INT_CODE, Result.STRING_IS_NOT_INT_MESSAGE);
        }
        
        int commit = -1;
        Building building = new Building();
        building.setBuilding_name(building_name);
        building.setBuilding_zhuang_danyuan(building_zhuangshu_danyuan);
        building.setCity_id(Short.valueOf(city_id));
        building.setCity_name(city_name);
        building.setDistrict_id(Short.valueOf(district_id));
        building.setDistrict_name(district_name);
        building.setStreet_name(street_name);
        building.setProvince_id(Byte.valueOf(province_id));
        building.setProvince_name(province_name);
        building.setElevator_num(Short.valueOf(elevator_num));
        building.setInput_time(WrenchUtil.nowTime());
        
        try
        {
            commit = dao.addBuilding(building);
        }
        catch (Exception e)
        {
            // TODO Auto-generated catch block
            logger.error("==> commit=dao.addBuilding(building) execute error ,building = {} , error = {}; ",
                building,
                e);
            throw new WrenchException(Result.DB_INSERT_ERROR_CODE, Result.DB_INSERT_ERROR_MESSAGE);
        }
        
        if (commit <= 0)
        {
            logger.error("==> commit=dao.addBuilding(building) execute success , but commit <=0 , commit = {} , building = {} ",
                commit,
                building);
            throw new WrenchException(Result.BUILDING_ADD_BUILDING_FAIL_CODE, Result.BUILDING_ADD_BUILDING_FAIL_MESSAGE);
        }
        
        logger.debug("==> commit=dao.addBuilding(building) execute success , commit = {} , building = {} ",
            commit,
            building);
        
        return commit;
        
    }
    
}
